/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sgsmcan.Dao;

import com.sgsmcan.Entidad.Perro;
import com.sgsmcan.Hibernate.HibernateUtil;
import com.sgsmcan.Utils.Utilitario;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author user
 */
public class PerroDaoImpl extends HibernateDao<Perro> implements PerroDao<Perro> {

    @Override
    public Perro getPerro(Integer id) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Perro.class, "perro")
                    .createAlias("perro.raza", "rza").createAlias("perro.color", "clo")
                    .createAlias("perro.amo", "mo", CriteriaSpecification.LEFT_JOIN).add(Restrictions.eq("perro.id", id));
            return (Perro) criteria.uniqueResult();
        } catch (HibernateException e) {
            Utilitario.LOG.error("Error en PerroDaoImpl", e);
            throw new HibernateException(e);
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    @Override
    public DetachedCriteria getPerros() {
        DetachedCriteria criteria = DetachedCriteria.forClass(Perro.class, "perro").createAlias("perro.raza", "rza").add(Restrictions.eq("perro.estado", Boolean.TRUE));
        return criteria;
    }

    public List<Perro> getPerrosss() {
        List<Perro> list = null;
        Session session = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("SELECT DISTINCT PE FROM Perro PE LEFT JOIN FETCH PE.serenoperros sp where sp.spk.perro.id IS NULL OR sp.estado = 0 AND PE.estado = 1");
            list = query.list();
        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return list;
    }

    public List<Perro> getPerroUpdate() {
        List<Perro> list = null;
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("FROM Perro where estado = 1");
            list = query.list();
        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return list;
    }

    @Override
    public List<String> autocomplete(String parametro) {
        List<String> nombresperros = null;
        Session session = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("SELECT pe.nombre FROM Perro AS pe WHERE pe.nombre LIKE :penom");

            query.setParameter("penom", "%" + parametro + "%");
            query.setMaxResults(10);
            nombresperros = query.list();

        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {   
            if (session != null) {
                session.close();
            }
        }
        return nombresperros;
    }

    @Override
    public List<Perro> autocompletar(String parametro) {
        List<Perro> list = null;
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("SELECT pe FROM Perro AS pe WHERE sp.spk.perro.id IS NULL and pe.nombre LIKE :parat");
            query.setParameter("parat", "%" + parametro + "%");
            query.setMaxResults(10);
            list = query.list();
        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return list;
    }

    @Override
    public Integer getPerroId(String parametro) {
        Integer idPerro = 0;
        Session sesion = null;
        List<Integer> idSe = null;
        try {
            sesion = HibernateUtil.getSessionFactory().openSession();
            Query query = sesion.createQuery("SELECT pe.id FROM Perro as pe WHERE pe.nombre LIKE :parat");
            query.setParameter("parat", "%" + parametro + "%");
            idSe = query.list();

            idPerro = idSe.get(0);

        } catch (HibernateException e) {
        } finally {
            if (sesion != null) {
                sesion.close();
            }
        }
        return idPerro;
    }
}
